package com.oa.hyperion.api.v1;

import com.auth0.jwt.interfaces.Claim;
import com.oa.hyperion.Global;
import com.oa.hyperion.entity.Attendance;
import com.oa.hyperion.entity.Controller;
import com.oa.hyperion.repository.ControllerMapper;
import com.oa.hyperion.service.AttenDanceService;
import com.oa.hyperion.utils.JwtUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

//@Api("考勤Api")
@RestController
@RequestMapping("/v1/Attendance")
public class AttendanceController {
    @Autowired
    AttenDanceService attenDanceService;
    @Autowired
    ControllerMapper controllerMapper;

    @PostMapping("/signIn")
    public Attendance signIn(@RequestHeader("Authorization") String token){
        Map map= JwtUtils.getClaim(token.substring(7));
        Claim claim= (Claim) map.get("id");
        Integer integer = claim.asInt();
        Attendance attendance = attenDanceService.SignIn(integer);
        return attendance;
    }

    @PostMapping("/signOut")
    public Attendance signOut(@RequestHeader("Authorization") String token){
        Map map= JwtUtils.getClaim(token.substring(7));
        Claim claim= (Claim) map.get("id");
        Integer integer = claim.asInt();
        return attenDanceService.SignOut(integer);
    }

    /**
     * 考勤异常查询
     */
    @PostMapping("/AttendanceExceptionSelect")
    public List<Attendance> AttendanceExceptionSelect(@RequestBody Attendance attendance){
        return attenDanceService.AttendanceExceptionSelect(attendance);
    }

    /**
     * 考勤异常统计
     */
    @PostMapping("/AttendanceExceptionStatistics")
    public List<Attendance> AttendanceExceptionStatistics(){
        Attendance attendance=new Attendance();
        attendance.setStatus("xz");
        return attenDanceService.AttendanceExceptionSelect(attendance);
    }

    /**
     * 考勤记录查询
     */
    @PostMapping("/AttendanceRecordSelect")
    public List<Attendance> AttendanceRecordSelect(@RequestBody Attendance attendance){
        if(attendance==null){
            attendance=new Attendance();
        }
        List<Attendance> list = attenDanceService.AttendanceExceptionSelect(attendance);
        return list;
    }

    /**
     * 自身考勤记录
     */
    @PostMapping("/employeeAttendanceRecord")
    public List<Attendance> employeeAttendanceRecord(@RequestHeader("Authorization") String token){
        Map map= JwtUtils.getClaim(token.substring(7));
        Claim claim= (Claim) map.get("id");
        Integer id = claim.asInt();
        Attendance attendance=new Attendance();
        attendance.setEmployee(id);
        return attenDanceService.AttendanceExceptionSelect(attendance);
    }

    /**
     * 补卡
     */
    @PostMapping("/Supplementary")
    public List<Attendance> Supplementary(@RequestBody Attendance attendance){
        attenDanceService.Supplementary(attendance);
        return null;
    }

    /**
     * 修改签到时间
     */
    @PostMapping("/SignInTimeChange")
    public List<Attendance> SignInTimeChange(@RequestBody Attendance attendance){
        System.out.println(attendance);
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(attendance.getSignIn());
        Global.signInHour=calendar.get(Calendar.HOUR_OF_DAY);
        Global.signInMin=calendar.get(Calendar.MINUTE);
        Global.signInSecond=calendar.get(Calendar.SECOND);
        return null;
    }

    /**
     * 修改签退时间
     */
    @PostMapping("/SignOutTimeChange")
    public List<Attendance> SignOutTimeChange(@RequestBody Attendance attendance){
        System.out.println(attendance);
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(attendance.getSignOut());
        Global.signOUtHour=calendar.get(Calendar.HOUR_OF_DAY);
        Global.signOutMin=calendar.get(Calendar.MINUTE);
        Global.signOutSecond=calendar.get(Calendar.SECOND);
        return null;
    }




    @PostMapping("/getControllers")
    public List<Controller> getControllers(){
        List<Controller> all = controllerMapper.getAll();
        return all;
    }
}
